package cn.web03.controller;

import cn.web03.dao.UserDao;
import cn.web03.pojo.User;
import cn.web03.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

@Controller
public class UserController {
    @ResponseBody
    @GetMapping("/user/list")
    public List<User> getUserList(){
        //获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // 方式1 推荐
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> userList = mapper.getUserList();
        //方式2
//        List<User> userList = sqlSession.selectList("cn.web03.dao.UserDao.getUserList");

        for (User user : userList){
            System.out.println(user);
        }

        sqlSession.close();
        return userList;

    }

}













